package ntu.metaweb.update;

import java.io.IOException;
import java.net.URL;

import rice.p2p.commonapi.rawserialization.InputBuffer;
import rice.p2p.commonapi.rawserialization.OutputBuffer;
import ntu.metaweb.XMLMarshaller;
import ntu.p2p.dht.messaging.DHTObjectDifference;

public class UpdateTagRating implements DHTObjectDifference {
	protected static final short TYPE = 5;
	
	protected URL url;
	protected String tag;
	protected String user;
	protected boolean up;
	
	public UpdateTagRating(URL url, String tag, String user, boolean up) {
		this.url = url;
		this.tag = tag;
		this.user = user;
		this.up = up;
	}
	
	public UpdateTagRating(InputBuffer buf) {
		try {
			this.url = (URL) XMLMarshaller.getInstance().unmarshall(buf.readUTF());
			this.tag = buf.readUTF();
			this.user = buf.readUTF();
			this.up = buf.readBoolean();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static DHTObjectDifference build(InputBuffer buf) {
		return new UpdateTagRating(buf);
	}
	
	@Override
	public void serialize(OutputBuffer buf) throws IOException {
		buf.writeShort(TYPE);
		buf.writeUTF(XMLMarshaller.getInstance().marshall(url));
		buf.writeUTF(tag);
		buf.writeUTF(user);
		buf.writeBoolean(up);
	}

}
